<?php

/**
 * Framework_Module_GuestBook
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Modules
 * @filesource
 */

/**
 * Framework_Module_GuestBook
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Modules
 */
class Framework_Module_GuestBook extends Framework_Module
{

	
	/**
     * $navParts
     *
     * @access      public
     * @var         array		$navParts      Url parts
     */
	private $navParts = array();
	
	
	/**
     * $cacheHander
     *
     * @access      public
     * @var         object		$$cacheHander     Instance of Framework_Cache_Handler_Module
     */
	private $cacheHander;

	/**
     * __construct
     *
     * Initializes the module: sets the default presenter, the allowed
     * conrollers, and the allowed events
     *
     * @access 		public
     * @return 		void
     */
	public function __construct()
	{
		parent::__construct();
		// Presenter
		if (Framework::$request->isAjax()) {
			$this->presenter = 'Module';
		 } else {
			$this->presenter = 'Smarty';
		 }
		// Inner template files
		$this->tplFile = 'GuestBook.tpl';
		// Set outer template files
		$this->pageTemplateFile = 'ProfilePage.tpl';
		// Valid Controllers
		$this->controllers[] = 'Peopletab';
			
		$this->navParts = Framework::$request->requestParts;
		
		
		 //Caching
		 $this->isCacheable = true;
		 $this->cacheHandlerClass = 'Module';

			
	}

	/**
     * __default
     *
     * Loads guestbook
     *
     * @access 		public
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	public function __default()
	{
		// Get Owner
		$owner = Framework_Owner::singleton();
			
			
		$this->cacheHandler = $this->getCacheHandler();
			
			
		// Form for adding new comment
		$form = Framework_Quickform::factory('guestBook', 'post', Framework::$request->getBaseHTTPPath().'/'.$owner->userName.'/GuestBook/');

		$form->addElement('textarea', 'message', 'Entry');

		$form->addElement('submit','btnSubmit','Submit Comment','class="button" id="btnSubmit"');
			
		// Filters
		$form->applyFilter('__ALL__','trim');
		$form->applyFilter('__ALL__',array(Framework::$util,'filter_removeXSS'));
			
			
		$this->forms['form'] = $form;
			
		// Process form
		if ($form->validate()) {
			$result = $form->process(array($this, 'process'));
			if (PEAR::isError($result)) {
				return $result;
			} else {
				$form->getElement('message')->setValue('');
			}
		} 
		
		


		// Check for delete
		// TODO:: needs to be an event
		if (isset($_GET['delete'])) {
			if (preg_match('/^[0-9]+/i', $_GET['delete'])) {
				$result = $this->delete($_GET['delete']);
				if (PEAR::isError($result)) {
					return $result;
				} else {
					$this->setData('deleted', 'Entry Deleted');
				}
			}

		}

		// Get the comments
		$queryArray = array(
			'sqlKey' => 'getGuestBookEntries',
			'params' => array($owner->idOwner)
		);
			
		$result = Framework_DB::dataGridFactory($owner->ownerDB, $queryArray, 15, 'page', $this->cacheHandler);
		if (!PEAR::isError($result)) {
			$data = $result->getData();
			if (!PEAR::isError($data) && count($data)) {
				$this->setData('entries', $data);
				$this->setData('idOwner', $owner->idOwner);
				$this->setData('pager', $result->getPager(true));
			} else {
				$this->setData('noResults', 'No Result');
			}


		} else {
			return $result;
		}
			
			

	}

	/**
     * process
     *
     *
     * @access 		public
     * @param 		array		$data		Data to process
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	public function process($data)
	{
		if ($this->user->userStatus == OWNER_USER_STATUS ||
		     $this->user->userStatus == NORMAL_USER_STATUS) {
			if ($data['message'] && strlen($data['message'])) {
				$owner = Framework_Owner::singleton();
			    		
				$guestBookTable = $owner->getTable('GuestBook');
					
				$guestBookTable->idOwner = $owner->idOwner;
				$guestBookTable->idSiteUser = $this->user->idSiteUser;
				$guestBookTable->message = $data['message'];
				$guestBookTable->dateAdded = time();
				$result = $guestBookTable->save();
				if (PEAR::isError($result)) {
					return $result;
				} else {
					return true;
				}
			} else {
				return PEAR::raiseError('Must Include Message');
			}
		}
	}

	/**
     * process
     *
     *
     * @access 		private
     * @param 		integer		$id			Message ID to delete
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	private function delete($id)
	{
		if ($this->user->userStatus == OWNER_USER_STATUS) {
			$owner = Framework_Owner::singleton();
			$where = array('idGuestBook' => $id, 'idOwner', $owner->idOwner);
			$result = $owner->delete('GuestBook', $where);
			if (PEAR::isError($result)) {
				return $result;
			} else {
				$this->setData('deleteConfirm', 'Message Deleted');
				return true;
			}

		}
	}


}

?>